package hu.dynamicwebshop.daos.impls;

import hu.dynamicwebshop.daos.MenuDAO;
import hu.dynamicwebshop.entities.Menu;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author Szecskó Zoltán <zoltan.szecsko@gmail.com>
 */
public class MenuDAOImpl implements Serializable, MenuDAO {

    private static final long serialVersionUID = 1L;
    private Session session;
    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public List<Menu> getMenuStructure() {
        examineSession();
        session.beginTransaction();

        @SuppressWarnings("unchecked")
        List<Menu> results = session.createCriteria(Menu.class).list();

        session.close();
        return results;
    }

    private void examineSession() {
        if (session == null) {
            setSession();
        } else if (!session.isOpen()) {
            session = session.getSessionFactory().openSession();
        }
    }

    private void setSession() {
        this.session = sessionFactory.openSession();
    }

    @Override
    public void saveMenu(Menu menu) {
        examineSession();
        session.beginTransaction();

        session.save(menu);

        session.getTransaction().commit();
        session.close();
    }

    @Override
    public int getLastPlaceId() {
        examineSession();
        session.beginTransaction();

        Criteria c = session.createCriteria(Menu.class);
        c.addOrder(Order.desc("placeId"));
        c.setMaxResults(1);

        Menu result = (Menu) c.uniqueResult();

        session.close();
        return result.getPlaceId();
    }

    @Override
    public void modifyMenu(Menu menu) {
        examineSession();
        session.beginTransaction();

        session.saveOrUpdate(menu);

        session.getTransaction().commit();
        session.close();
    }

    @Override
    public int getPlaceIdByMenuName(String menuName) {
        examineSession();
        session.beginTransaction();

        Criteria c = session.createCriteria(Menu.class)
                .add(Restrictions.like("menuName", menuName));

        Menu result = (Menu) c.list().get(0);

        session.close();
        return result.getPlaceId();
    }

    @Override
    public int getMenuIdByName(String menuName) {
        examineSession();
        session.beginTransaction();

        Criteria c = session.createCriteria(Menu.class)
                .add(Restrictions.like("menuName", menuName));

        Menu result = (Menu) c.list().get(0);
        session.close();

        if (result == null) {
            return 0;
        } else {
            return result.getMenuId();
        }
    }
}
